﻿using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;

namespace FRMS.BUS
{
    public class HomeBUS
    {
        public ResponseMessage QueryMenu()
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            List<MenuInfo> list = da.QueryMenuInfo();

            message.errcode = 0;
            message.errmsg = "";
            message.data = list;
            message.total = list.Count;

            return message;
        }

        public ResponseMessage Login(string LoginName, string LoginPwd, JwtSettings _jwtSettings, IHttpContextAccessor _access)
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            User user = da.GetUser(LoginName, LoginPwd);
            if (user != null)
            {
                _access.HttpContext.Session.SetString("UserId", user.ID.ToString());
                user.Token = MakeToken(user, _jwtSettings);
                message.errcode = 0;
                message.errmsg = "";
                message.data = user;
            }
            else
            {
                message.errcode = 1;
                message.errmsg = "登录失败";
            }



            return message;
        }

        private string MakeToken(User info, JwtSettings _jwtSettings)
        {
            string strToken = "";
            var claim = new Claim[]{
                   new Claim(ClaimTypes.Name,info.LoginName),
                   new Claim("UserCode",info.ID.ToString()),
                   new Claim("UserName",info.LoginName)
            };
            var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey));
            var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var token = new JwtSecurityToken
            (
                issuer: _jwtSettings.Issuer,
                audience: _jwtSettings.Audience,
                claims: claim,
                notBefore: DateTime.Now,
                expires: DateTime.Now.AddDays(2),//过期时间                
                signingCredentials: creds
            );
            try
            {
                strToken = new JwtSecurityTokenHandler().WriteToken(token);
            }
            catch (Exception ee) { return ""; }

            return strToken;
        }

        public ResponseMessage LoadUser(int id)
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            User user = da.GetUser(id);

            message.errcode = 0;
            message.errmsg = "";
            message.data = user;

            return message;
        }

        public ResponseMessage GetMenuByProject(int projectid, string name)
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            List<Menu> list = da.GetMenuByPid(projectid, name);

            message.errcode = 0;
            message.errmsg = "";
            message.data = list;
            message.total = list.Count;

            return message;
        }

        public ResponseMessage GetMenuByProject(int projectid)
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            List<Menu> list = da.GetMenuByPid(projectid);

            message.errcode = 0;
            message.errmsg = "";
            message.data = list;
            message.total = list.Count;

            return message;
        }

        public ResponseMessage GetFoption(int userid, string url,string username)
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            List<FOption> list = da.GetFoptionByUser(userid, url, username);

            message.errcode = 0;
            message.errmsg = "";
            message.data = list;
            message.total = list.Count;

            return message;
        }

        public ResponseMessage GetAllFoption(int userid)
        {
            ResponseMessage message = new ResponseMessage();
            HomeDAL da = new HomeDAL();
            List<FOption> list = da.GetAllFoption(userid);

            message.errcode = 0;
            message.errmsg = "";
            message.data = list;
            message.total = list.Count;

            return message;
        }
    }
}
